//
//  EAMainController.m
//  WhatToEat
//
//  Created by 方力 on 15/7/30.
//  Copyright (c) 2015年 Mac. All rights reserved.
//

#import "EAMainController.h"
#import "FLVolSlide.h"
#import "EAChooseMatch.h"

@interface EAMainController ()
@property(strong,nonatomic)NSMutableArray * dataSource;
@property(strong,nonatomic)NSArray * meals;
@property(strong,nonatomic)FLVolSlide * slide;



@property(copy,nonatomic)NSString * mealtype;
@property(copy,nonatomic)NSString * aty_level;
@property(nonatomic)CGFloat level;
@end

@implementation EAMainController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    
    [self updateUI];
    
    self.level=0;

    
    
}



-(void)updateUI
{
    
    self.navigationItem.title=@"吃神马";
    
    



    self.navigationController.navigationBar.barStyle = UIStatusBarStyleDefault;
    [self.navigationController.navigationBar setTintColor:[UIColor whiteColor]];

    self.tableView.backgroundColor=[UIColor groupTableViewBackgroundColor];
    

    self.dataSource=[@[
                      
                      @"0",
                      
                      @"0"
                      
                      
                      
                      ] mutableCopy];
    
    
    self.meals=@[@"早餐",@"中餐",@"晚餐"];
    
}

-(void)viewWillAppear:(BOOL)animated
{

    [super viewWillAppear:animated];

    self.dataSource=[@[
                       
                       @"0",
                       
                       @"0"
                       
                       
                       
                       ] mutableCopy];
    
    
    [self.tableView reloadData];
}


- (void)toLeft:(UIButton *)sender
{
    
    [ApplicationDelegate.ddVc showLeftController:YES];
}





#pragma mark-----tableView

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{

    return 3;

}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section==0) {
        
        return kUI_SCREEN_WIDTH*273/560;
    }
    
    if (section==1) {
        
        return kUI_SCREEN_WIDTH*268/551;
    }
    
    if (section==2) {
        return kUI_SCREEN_HEIGHT-64-44-kUI_SCREEN_WIDTH*273/560-kUI_SCREEN_WIDTH*268/551;
    }
    
    
    return 120;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{

   return  [self.dataSource[indexPath.section] floatValue];
    
    
    

    return 0;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    if (section==0) {
        
        return 3;
        
    }
    
    if (section==1) {
        return 1;
    }
    

    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    UITableViewCell * cell=[[UITableViewCell alloc] initWithStyle:0 reuseIdentifier:@"Cell"];
    
    if (indexPath.section==0) {
        
        cell.textLabel.text=self.meals[indexPath.row];
        
        
        
    }
    

    if (indexPath.section==1) {
        
    
        
        self.slide=[[FLVolSlide alloc] initWithFrame:CGRectMake(10, 40, kUI_SCREEN_WIDTH-20, 18)];
     
        self.slide.slideView.value=self.level;
        
        self.slide.processView.frame = CGRectMake(0, 0, self.slide.width * self.level, self.slide.height);
        
        
        [[self.slide.slideView rac_signalForControlEvents:UIControlEventValueChanged] subscribeNext:^(UISlider * x) {
            
            self.level=x.value;
            
            CGFloat levelFloat=self.level*3;
            
            self.aty_level=[Commtools decimalwithFormat:@"0" floatV:levelFloat];
            
        
        }];
        
        
        self.slide.slideValueChageBlock=^(UISlider * x){
        
            self.level=x.value;
            
            self.aty_level=[Commtools decimalwithFormat:@"0" floatV:self.level];
        
        };
        
        
        [cell addSubview:self.slide];
        
        
        UILabel * l=[[UILabel alloc] initWithFrame:CGRectMake(10, self.slide.bottom+20, 40, 22)];
        l.text=@"舒适";
        
        l.font=[UIFont fontWithName:RandW5 size:15];
        l.textColor=[UIColor darkGrayColor];
        [cell addSubview:l];
        
        UILabel * l1=[[UILabel alloc] initWithFrame:CGRectMake(self.slide.width/2-10, self.slide.bottom+20, 40, 22)];
        l1.text=@"一般";
        
         l1.font=[UIFont fontWithName:RandW5 size:15];
        l1.textColor=[UIColor darkGrayColor];
        [cell addSubview:l1];
        
        
        
        UILabel * l2=[[UILabel alloc] initWithFrame:CGRectMake(self.slide.width-30, self.slide.bottom+20, 40, 22)];
        l2.text=@"较累";
        l2.font=[UIFont fontWithName:RandW5 size:15];
        l2.textColor=[UIColor darkGrayColor];
        [cell addSubview:l2];
        
        
//        UILabel * l3=[[UILabel alloc] initWithFrame:CGRectMake(self.slide.width-40, self.slide.bottom+20, 46, 22)];
//        l3.text=@"非常累";
//        l3.font=[UIFont fontWithName:RandW5 size:15];
//        l3.textColor=[UIColor darkGrayColor];
//        [cell addSubview:l3];
    }
    
    
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    return cell;
    

}





- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{


    UIView * v=[[UIView alloc] init];


    UIImageView *iv=[[UIImageView alloc] init];
    

    
    switch (section) {
        case 0:
        {
            v.frame=CGRectMake(0, 0, kUI_SCREEN_WIDTH, kUI_SCREEN_WIDTH*273/560);
            
      
            
            iv.frame= CGRectMake(0, 0, kUI_SCREEN_WIDTH, kUI_SCREEN_WIDTH*273/560+10);
            iv.image=[UIImage imageNamed:@"shizhong"];
            
            

        
        }
            break;
            
            case 1:
        {
             v.frame=CGRectMake(0, 0, kUI_SCREEN_WIDTH, kUI_SCREEN_WIDTH*268/551);

            
            iv.frame=CGRectMake(0, 0, kUI_SCREEN_WIDTH, kUI_SCREEN_WIDTH*268/551+10);
            iv.image=[UIImage imageNamed:@"pilao"];
            
    
        }
            break;
            case 2:
        
        {
        
         v.frame=CGRectMake(0, 0, kUI_SCREEN_WIDTH, kUI_SCREEN_HEIGHT-64-44-kUI_SCREEN_WIDTH*273/560-kUI_SCREEN_WIDTH*268/551);
            
            iv.frame=v.bounds;
            iv.image=[UIImage imageNamed:@"shanshi"];
           
        }
            break;
        default:
            break;
    }
    UITapGestureRecognizer * tp=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(actions:)];
    
    iv.userInteractionEnabled=YES;
    [iv addGestureRecognizer:tp];
        iv.tag=section;

   [v addSubview:iv];
    

    
    
    
    
    return v;

}

-(void)actions:(UITapGestureRecognizer *)t
{
   
    
    UIView * v=t.view;
    
    
    switch (v.tag) {
        case 0:
            if ([self.dataSource[0] isEqualToString:@"44"]) {
                
                
                
                [self.dataSource replaceObjectAtIndex:0 withObject:@"0"];
            }else
            {
            
             [self.dataSource replaceObjectAtIndex:0 withObject:@"44"];
            }
            
            
            
            break;
        case 1:
            
            if ([self.dataSource[1] isEqualToString:@"130"]) {
                
                  [ApplicationDelegate.ddVc setEnableGesture:NO];
                
                [self.dataSource replaceObjectAtIndex:1 withObject:@"0"];
            }else
            {
                
                [self.dataSource replaceObjectAtIndex:1 withObject:@"130"];

                [ApplicationDelegate.ddVc setEnableGesture:NO];

            }
            
            
            break;
        case 2:
            
        {
            
            {
                
                if (!self.mealtype) {
                    [SVProgressHUD showImage:nil status:@"请选择用餐类别"];
                    return;
                    
                }
                
                if (!self.aty_level) {
                    [SVProgressHUD showImage:nil status:@"身体疲劳度"];
                    return;
                    
                }
                
                
                
            
                UIStoryboard *st=[UIStoryboard storyboardWithName:@"Main" bundle:nil];
                
                
                EAChooseMatch *cVC=[st instantiateViewControllerWithIdentifier:@"EAChooseMatch"];
                
                
                cVC.mealtype=self.mealtype;
                cVC.aty_level=self.aty_level;
                
        

                

                [self.navigationController pushViewController:cVC animated:YES];
              
            }
            
        }
            
            break;
            
        default:
            break;
    }



    [self.tableView reloadData];

}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    if (indexPath.section==0) {
        
        if ([self.dataSource[0] isEqualToString:@"44"]) {
            
            
            switch (indexPath.row) {
                case 0:
                    [SVProgressHUD showImage:nil status:@"你选择了早餐"];
                    break;
                case 1:
                            [SVProgressHUD showImage:nil status:@"你选择了午餐"];
                    break;
                case 2:
                            [SVProgressHUD showImage:nil status:@"你选择了晚餐"];
                    break;
                    
                default:
                    break;
            }
            
            
            self.mealtype=[NSString stringWithFormat:@"%ld",(long)indexPath.row+1];
            
            
            [self.dataSource replaceObjectAtIndex:0 withObject:@"0"];
            
            
            
            
            
        }else
        {
            
            [self.dataSource replaceObjectAtIndex:0 withObject:@"44"];
        }
        
        
    }




    [self.tableView reloadData];

}


//-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
//{
//
//
//
//}

@end
